<template>
  <div v-loading="loading">
    <el-form ref="form" :model="form" :rules="rules" label-width="120px" class="dataForm">
      <el-row>
        <el-col :span="12">
          <el-form-item :label="$t('流水号')" prop="serialNumber">
            <el-input v-model="form.serialNumber" :placeholder="$t('保存后自动生成')" show-word-limit disabled />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('开放控制')">
            <dict-select
              v-model="form.openControl"
              dictionary="open_control"
              :placeholder="$t('开放控制')"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('密级')">
            <dict-select
              v-model="form.securityClassification"
              dictionary="security_classification"
              :placeholder="$t('密级')"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('缓急')">
            <dict-select
              v-model="form.gradual"
              dictionary="gradual"
              :placeholder="$t('缓急')"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('登记号')" prop="registrationNumber">
            <el-input v-model="form.registrationNumber" :placeholder="$t('登记号')" show-word-limit maxlength="100" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('收文日期')" prop="dateReceipt">
            <el-date-picker
              v-model="form.dateReceipt"
              clearable
              style="width: 200px"
              type="date"
              value-format="yyyy-MM-dd"
              :placeholder="$t('收文日期')"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('主送领导')" prop="mainLead">
            <UserSelect v-model="form.mainLead" :placeholder="$t('主送领导')" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('文件标题')" prop="actTitle">
            <el-input v-model="form.actTitle" :placeholder="$t('文件标题')" show-word-limit maxlength="100" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('文号')" prop="referenceNumber">
            <el-input v-model="form.referenceNumber" :placeholder="$t('文号')" show-word-limit maxlength="100" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('来文单位')" prop="jeremyLevinUnit">
            <el-input v-model="form.jeremyLevinUnit" :placeholder="$t('来文单位')" show-word-limit maxlength="100" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('主办')" prop="hostUserId">
            <UserSelect v-model="form.hostUserId" :placeholder="$t('主办')" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('协办')" prop="jointlyUserId">
            <UserSelect
              v-model="form.jointlyUserId"
              :multiple="true"
              :placeholder="$t('随行人员')"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('传阅')" prop="circulateUserId">
            <UserSelect v-model="form.circulateUserId" :multiple="true" :placeholder="$t('传阅')" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('办理期限')" prop="dealWithTime">
            <el-date-picker
              v-model="form.dealWithTime"
              clearable
              style="width: 200px"
              type="datetime"
              value-format="yyyy-MM-dd HH:mm:ss"
              format="yyyy-MM-dd HH:mm:ss"
              :placeholder="$t('办理期限')"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('复文时间')" prop="fuwenTime">
            <el-date-picker
              v-model="form.fuwenTime"
              clearable
              style="width: 200px"
              type="date"
              value-format="yyyy-MM-dd"
              :placeholder="$t('复文时间')"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('签报时间')" prop="signAtTime">
            <el-date-picker
              v-model="form.signAtTime"
              clearable
              style="width: 200px"
              type="date"
              value-format="yyyy-MM-dd"
              :placeholder="$t('签报时间')"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('督办时间')" prop="overseeTheTime">
            <el-date-picker
              v-model="form.overseeTheTime"
              clearable
              style="width: 200px"
              type="date"
              value-format="yyyy-MM-dd"
              :placeholder="$t('督办时间')"
            />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item :label="$t('备注')" prop="remark">
            <el-input v-model="form.remark" type="textarea" :placeholder="$t('备注')" show-word-limit maxlength="1000" />
          </el-form-item>
        </el-col>
      </el-row>
      <FileTable :list.sync="form.fileList" data-type="oa_addressee" :btn-show="!otherBtnHide" />
    </el-form>
    <div slot="footer" class="dialog-footer" style="text-align: right;">
      <!--保存，保存并启动流程，取消 三个按钮-->
      <FormBtn ref="formBtn" :bus-data="form" has-permi="oa:addressee" :btn-hide="btnHide" :menu-key="menuKey" />
    </div>
  </div>
</template>

<script>
    import { saveOaAddressee, getOaAddressee } from '@/api/oa/addressee/oaAddressee'
    export default {
        data() {
            return {
              // 菜单标识
              menuKey: 'oa_addressee',
                // 是否隐藏其它按钮
               otherBtnHide: false,
                // 是否隐藏保存按钮
                btnHide: false,
                // 流程状态字典
                actStatusOptions: [],
                openControlOptions: [],
                securityClassificationOptions: [],
                gradualOptions: [],
                // 表单参数
                form: {},
                // 表单校验
                rules: {
                    dateReceipt: [
                        { required: true, message: this.$t('必填信息'), trigger: 'blur' }
                    ],
                    actTitle: [
                        { required: true, message: this.$t('必填信息'), trigger: 'blur' }
                    ],
                    jeremyLevinUnit: [
                        { required: true, message: this.$t('必填信息'), trigger: 'blur' }
                    ]
                },
                // 遮罩层
                loading: false,
                // 操作按钮默认不展示
                buttonShow: true
            }
        },
        created() {
            this.getDicts('open_control').then(response => {
                this.openControlOptions = response.data
            })
            this.getDicts('security_classification').then(response => {
                this.securityClassificationOptions = response.data
            })
            this.getDicts('gradual').then(response => {
                this.gradualOptions = response.data
            })
            this.getDicts('work_flow_status').then(response => {
                this.actStatusOptions = response.data
            })
            this.$bus.$emit('async_done')
        },
        methods: {
            // 初始化数据
            init(data) {
                this.loading = true
                if (data != null && data.id !== undefined) {
                    getOaAddressee(data.id).then(response => {
                        this.form = response.data
                      if (response.data.actStatus !== 'D' && response.data.actStatus !== 'W') {
                        this.buttonShow = true
                      }
                        this.loading = false
                    })
                } else {
                    this.reset()
                    this.loading = false
                }
            },
            // 表单重置
            reset() {
                this.form = {
                    id: undefined,

                    serialNumber: undefined,

                    openControl: undefined,

                    securityClassification: undefined,

                    gradual: undefined,

                    registrationNumber: undefined,

                    dateReceipt: undefined,

                    actTitle: undefined,

                    referenceNumber: undefined,

                    jeremyLevinUnit: undefined,

                    hostUserId: undefined,

                    jointlyUserId: undefined,

                    circulateUserId: undefined,

                    dealWithTime: undefined,

                    fuwenTime: undefined,

                    signAtTime: undefined,

                    overseeTheTime: undefined,

                    remark: undefined,

                    actId: undefined,

                    actStatus: undefined,

                    createBy: undefined,

                    createTime: undefined,

                    updateBy: undefined,

                    updateTime: undefined,

                    delFlag: undefined,
                    fileList: [],
                  mainLead: undefined
                }
                this.resetForm('form')
            },
            // 取消按钮
            cancel() {
                this.$parent.$parent.formOpen = false
                this.$parent.$parent.reset()
            },
            // 关闭form页面
            closeForm() {
                // 关闭form页面遮罩层
                this.loading = false
                // 关闭form页面
                if (this.$parent.$parent.formOpen !== undefined) {
                    this.$parent.$parent.formOpen = false
                }
                // 刷新list页面
                if (this.$parent.$parent.getList !== undefined) {
                  this.$parent.$parent.getList()
                }
            },
          /** 提交按钮 */
          submitForm: function(type) {
            return new Promise((resolve, reject) => {
              this.$refs['form'].validate(valid => {
                // 验证数据
                if (valid) {
                  this.loading = true
                  // 保存数据
                  saveOaAddressee(this.form).then(response => {
                    if (response.code === 200) {
                      if (type === 'process') {
                        // 设置数据id方式启动流程后取消并再次启动会创建多条记录
                        this.form.id = response.data
                        // 启动流程
                        this.startProcess(response.data)
                        debugger
                      } else {
                        this.msgSuccess(this.$t('保存成功'))
                        this.closeForm()
                      }
                      resolve()
                    } else {
                      this.msgError(response.msg)
                      this.loading = false
                      reject()
                    }
                  }).catch(() => {
                    this.loading = false
                  })
                } else {
                  reject()
                  this.locationError(document)
                }
              })
            })
          },
            // 启动流程
           startProcess: function(dataId) {
               this.loading = false
               // 启动流程时候传入的参数
               const processData = {}
               // 待办标题
               processData.title = this.form.actTitle
               // 业务表名
               processData.dataTable = 'oa_addressee'
               // 业务ID
               processData.dataId = dataId
               // 业务表单地址(待办页面需要打开的页面)
               processData.formPath = '@/views/oa/addressee/oaAddresseeForm'
               // 业务表单打印地址
               processData.viewPath = '@/views/oa/addressee/oaAddresseeView'
               // 菜单标识(根据菜单标识判断的走哪个流程)
               processData.menuKey = this.menuKey
               this.startProcessByMenu(processData, () => {
                   this.form.actStatus = 'D'
                   this.msgSuccess(this.$t('数据保存成功,流程启动成功'))
                   this.closeForm()
               })
           }

        }

    }
</script>
